﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using MPMovieCatalog.Configuration;
using MPMovieCatalog.Domain;
using MPMovieCatalog.Service;
using MPMovieCatalog.Util;

namespace MPMovieCatalog.Export.Forms
{
    public partial class AMCExportForm : Form
    {
        private List<RadioButton> m_moviesButtonGroup = new List<RadioButton>();

        public AMCExportForm()
        {
            InitializeComponent();
            UpdateUI();

            m_moviesButtonGroup.Add(allMoviesRadioButton);
            m_moviesButtonGroup.Add(selectedMoviesRadioButton);
        }

        public ArrayList SelectedMovies { get; set; }

        public long AllMoviesCount { get; set; }

        private void AMCExportForm_Load(object sender, EventArgs e)
        {
            ConfigurationWrapper wrapper = new ConfigurationWrapper();
            fileTextBox.Text = wrapper.AMCExportSection.FileName;

            SetSelectedIndex(m_moviesButtonGroup, wrapper.AMCExportSection.Movies);

            allMoviesRadioButton.Text = string.Format(ResourceUtils.GetString("export", 1), AllMoviesCount);
            selectedMoviesRadioButton.Text = string.Format(ResourceUtils.GetString("export", 2), SelectedMovies == null ? 0 : SelectedMovies.Count);

            EncodingInfo[] encodingInfos = Encoding.GetEncodings();
            Array.Sort(encodingInfos, this.CompareEncodingInfo);

            encodingComboBox.DataSource = encodingInfos;
            encodingComboBox.DisplayMember = "DisplayName";

            string encodingName = wrapper.AMCExportSection.Encoding;
            foreach (EncodingInfo encodingInfo in encodingInfos)
            {
                if (encodingInfo.Name == encodingName)
                {
                    encodingComboBox.SelectedItem = encodingInfo;
                    break;
                }
            }
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                fileTextBox.Text = saveFileDialog.FileName;
            }
        }

        private void AMCExportForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            ConfigurationWrapper wrapper = new ConfigurationWrapper();
            wrapper.AMCExportSection.FileName = fileTextBox.Text;
            wrapper.AMCExportSection.Encoding = ((EncodingInfo)encodingComboBox.SelectedItem).Name;
            wrapper.AMCExportSection.Movies = GetSelectedIndex(m_moviesButtonGroup);

            wrapper.SaveConfiguration();

        }

        private void SetSelectedIndex(List<RadioButton> buttonGroup, int index)
        {
            if (index >= 0 && index < buttonGroup.Count)
            {
                buttonGroup[index].Checked = true;
            }
        }

        private int GetSelectedIndex(List<RadioButton> buttonGroup)
        {
            int index = -1;
            foreach (RadioButton radioButton in buttonGroup)
            {
                if (radioButton.Checked)
                {
                    index = buttonGroup.IndexOf(radioButton);
                }
            }
            return index;
        }

        private void exportButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(fileTextBox.Text))
            {
                MessageBox.Show(this, ResourceUtils.GetString("export", 8), ResourceUtils.GetString("common", 3), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                fileTextBox.Focus();
                return;
            }

            if (File.Exists(fileTextBox.Text) && MessageBox.Show(this, string.Format(ResourceUtils.GetString("export", 9), fileTextBox.Text), ResourceUtils.GetString("common", 1), MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
            {
                return;
            }

            ArrayList ids = null;
            if (selectedMoviesRadioButton.Checked)
            {
                ids = new ArrayList();
                if (SelectedMovies != null && SelectedMovies.Count > 0)
                {
                    foreach (MovieInfo movieInfo in SelectedMovies)
                    {
                        ids.Add(movieInfo.Id);
                    }
                }
                else
                {
                    ids.Add(0);
                }
            }
            ArrayList movies = MovieServiceFactory.getMovieService().FindMovieInfoByIds(ids);

            AMCExporter amcExporter = new AMCExporter(movies, fileTextBox.Text, ((EncodingInfo)encodingComboBox.SelectedItem).Name);
            using (ProgressWorker progressWorker = new ProgressWorker(amcExporter))
            {
                progressWorker.Start();
            }

            DialogResult = DialogResult.OK;
            Close();
        }

        private void UpdateUI()
        {
            this.Text = ResourceUtils.GetString("amcExport", 0);
            fileLabel.Text = ResourceUtils.GetString("amcExport", 1);
            browseButton.Text = ResourceUtils.GetString("amcExport", 2);
            encodingLabel.Text = ResourceUtils.GetString("amcExport", 3);

            moviesGroupBox.Text = ResourceUtils.GetString("export", 0);
            allMoviesRadioButton.Text = ResourceUtils.GetString("export", 1);
            selectedMoviesRadioButton.Text = ResourceUtils.GetString("export", 2);
            exportButton.Text = ResourceUtils.GetString("export", 3);
            cancelButton.Text = ResourceUtils.GetString("export", 4);
        }

        public int CompareEncodingInfo(EncodingInfo x, EncodingInfo y)
        {
            return x.DisplayName.CompareTo(y.DisplayName);
        }
    }
}
